package com.niodata.dt.fs;

import java.io.IOException;
import java.util.Map;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;

public class RelLocalFileSystemBuilder implements FileSystemBuilder {

  @Override
  public FileSystem createFileSystem(String user, Map<String, String> conf)
        throws IOException {
    Configuration config = new Configuration();
    config.set(DpRelativeLocalFileSystem.FS_DP_REL_LOCAL_ROOTDIR,
        conf.get(DpRelativeLocalFileSystem.FS_DP_REL_LOCAL_ROOTDIR));
    config.set("fs.dpRelLocal.impl",
        DpRelativeLocalFileSystem.class.getName());
    String osName = System.getProperty("os.name");
    String rootDir = conf.get(DpRelativeLocalFileSystem.FS_DP_REL_LOCAL_ROOTDIR);
    if (osName.toLowerCase().contains("window")) {
      rootDir = rootDir.replace("\\", "/");
    }
    if (!rootDir.startsWith("/")) {
      rootDir = "/" + rootDir;
    }
    config.set(DpRelativeLocalFileSystem.FS_DP_REL_LOCAL_ROOTDIR, rootDir);
    config.set("fs.defaultFS", "dpRelLocal://" + rootDir);
    FileSystem local = FileSystem.newInstance(config);
    return local;
  }
}
